<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=OcfaB1xWOneDXgTmyTYwjGBizus7bW6E"></script>

    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
    <style type="text/css">
        html{height:100%}
        body{height:100%;margin:0px;padding:0px}
        #container{margin:0 0 3px;height:100%;}

        #l-map{height: 500px;width:100%;}
        #r-result{width:100%;}

        .tangram-suggestion-main {
            z-index: 999999;
        }
    </style>

</head>
<body class="hold-transition">

    <div id="app">

        <div class="content-header">
            <h1>首页
                <small>公司分布图</small>
            </h1>
            <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="app-container">

            <template>
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="分布图" name="first" >
                        <div class="filter-container" style="height: 420px">
                            <div id="container" ></div>
                        </div>
                    </el-tab-pane>

                    <el-tab-pane label="详细信息" name="second">

                        <!-- 搜索栏 -->
                        <div class="filter-container">
                            <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;"
                                      class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                            <el-button @click="findPage()" class="dalfBut">查询</el-button>
                            <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        </div>
                        <!-- 列表 -->
                        <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                            <el-table-column type="index" align="center" label="序号"></el-table-column>
                            <el-table-column prop="code" label="编码" align="center"></el-table-column>
                            <el-table-column prop="address" label="公司地址" align="center"></el-table-column>
                            <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
                            <el-table-column prop="latitude" label="所在维度" align="center"></el-table-column>

                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <!-- 分页条 -->
                        <div class="pagination-container">
                                <el-pagination
                                        class="pagiantion"
                                        @current-change="handleCurrentChange"
                                        :current-page="pagination.currentPage"
                                        :page-size="pagination.pageSize"
                                        layout="total, prev, pager, next, jumper"
                                        :total="pagination.total">
                                </el-pagination>
                            </div>

                    </el-tab-pane>

                </el-tabs>
            </template>
        </div>

        <!-- 新建标签弹层 -->
        <el-dialog title="新增地址" :visible.sync="dialogFormVisible">
            <el-form :inline="true"  :model="formInline" class="demo-form-inline">
                <el-form-item >
                    <div id="r-result">
                        <el-input v-model="formInline.address" id="suggestId" ></el-input>
                    </div>
                    <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                </el-form-item>
                <el-form-item >
                    <el-button type="primary" @click="onSubmit">确认添加</el-button>
                </el-form-item>
                <div id="l-map"></div>
            </el-form>
        </el-dialog>

    </div>
</body>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 6,
                total: 13,
                queryString: null,
            },
            addressList:[],// 地址列表
            opts: {
                width : 250,     // 信息窗口宽度
                height: 100,     // 信息窗口高度
                title : "详细信息"  // 信息窗口标题
            },
            dataList: [],//列表数据
            formData: {
                address:''
            },//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            checkitemIds: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            rules: {//校验规则
                code: [{required: true, message: '项目组编码为必填项', trigger: 'blur'}],
                name: [{required: true, message: '项目组名称为必填项', trigger: 'blur'}]
            },
            formInline: {
                code:'',
                address: '',
                longitude:'',
                latitude:''
            }
        },
        created() {

        },

        mounted(){
            this.createMap();
        },
        methods: {

            // 提交表单
            onSubmit() {
                axios.post("/address/add.do",this.formInline).then((response)=>{
                    if (response.data.flag){
                        this.$message({
                            message : response.data.message,
                            type:'success'
                        })
                        this.dialogFormVisible = false;
                        this.findPage();
                    } else {
                        this.$message.error(response.data.message)
                    }
                })


            },

            // 创建地图实例
            createMap(){
                axios.get("/address/findAll.do").then((response)=>{
                    if (response.data.flag){
                        this.addressList=response.data.data;
                        // 创建地图实例
                        var map = new BMap.Map("container");

                        map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
                        for (let i = 0; i<this.addressList.length ; i++) {
                            let address = this.addressList[i];
                            let point = new BMap.Point(address.longitude,address.latitude);
                            if (i == 0){
                                // 初始化地图，设置中心点坐标和地图级别
                                map.centerAndZoom(point, 10);
                            }
                            let marker = new BMap.Marker(point);// 创建标记对象
                            let infoWindow = new BMap.InfoWindow(address.address, this.opts);  // 创建信息窗口对象

                            // 绑定单击事件，显示详细信息
                            marker.addEventListener("click", function(){
                                // alert("您点击了标注");
                                map.openInfoWindow(infoWindow, point);      // 打开信息窗口
                            });
                            map.addOverlay(marker);// 显示标记对象
                        }
                    } else {
                        this.$message.error(response.data.message);
                    }
                });

            },

            // 弹出添加窗口
            handleCreate() {

                var _this = this;

                this.dialogFormVisible = true;
                this.formInline.address = '';

                // 百度地图API功能
                function G(id) {
                    return document.getElementById(id);
                }

                var map = new BMap.Map("l-map");
                map.centerAndZoom("武汉",12);                   // 初始化地图,设置城市和地图级别。
                map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {"input" : "suggestId"
                        ,"location" : map
                    });

                ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel").innerHTML = str;
                });

                var myValue;
                ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件

                    var _value = e.item.value;
                    console.log(_value);
                    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

                    _this.formInline.address = myValue;

                    setPlace();
                });
                var pp;
                var myGeo = new BMap.Geocoder();

                function setPlace(){
                    map.clearOverlays();    //清除地图上所有覆盖物
                    function myFun(){
                        pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        map.centerAndZoom(pp, 10);
                        map.addOverlay(new BMap.Marker(pp));    //添加标注

                        // 根据坐标得到地址描述
                        myGeo.getLocation(pp, function(result){
                            if (result){
                                _this.formInline.longitude = pp.lng;
                                _this.formInline.latitude = pp.lat;
                            }
                        });

                    }
                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: myFun
                    });
                    local.search(myValue);
                }
            },



            // 切换标签
            handleClick(tab, event) {
                console.log(tab, event);
                if (this.activeName == 'first' ){
                    this.createMap();
                } else {
                    this.findPage();
                }
            },

            //分页查询
            findPage() {
                //封装请求参数
                var params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送ajax请求
              axios.post("/address/findPage.do",params).then((response)=>{
               this.dataList = response.data.rows;
               this.pagination.total=response.data.total;
              })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            // 删除
            handleDelete(row) {
                this.$confirm('确认删除此纪录？','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                }).then(()=>{
                    // 提交删除
                    axios.get("/address/delete.do?id="+row.id).then((response)=>{
                        if (response.data.flag){
                            this.$message({
                                message:response.data.message,
                                ty: 'success'
                            });
                            this.findPage();
                        } else {
                            this.$message.error(response.data.message)
                        }
                    })
                }).catch(()=>{
                    this.$message({
                        message:'已取消删除',
                        type:'info'
                    })
                })
            }
        }
    })

</script>
</html>
